from api.medicine import Medicine
import pytest
from api.utils import Utils
import allure
import logging

#写一个工厂类，来产生唯一的药品数据

logger = logging.getLogger(__name__)

@pytest.fixture(scope='module')
def make_obj():
    mdc_obj = Medicine()
    print('创建medicine对象')
    yield mdc_obj

@pytest.fixture
def delete_medicine_in_teardown(make_obj):
    print('添加了药品，需要删除药品')
    yield make_obj
    print(f'执行add_medicine的teardown方法,药品的id是:{make_obj.id}')
    rs = make_obj.delete_medicine(make_obj.id)
    print(f'删除成功，{rs.json()}')

 
class Test_medicine_1:
    data = Utils.yaml_load('case/login_test.yaml')

    @allure.feature('登陆展示产品1')
    @pytest.mark.parametrize('pagesize,pagenum',Utils.yaml_data(data,'test_medicine_list'))
    def test_medicine_001(self,make_obj,pagesize,pagenum):
        rs = make_obj.list_medicine(pagesize,pagenum)
        print(rs.text)
        logger.info(rs.text)
        assert rs.status_code == 200

class Test_medicine_2:
    data = Utils.yaml_load('case/login_test.yaml')

    @allure.feature('登陆展示产品2-步骤分离')
    @pytest.mark.parametrize('pagesize,pagenum',Utils.yaml_data(data,'test_medicine_list'))
    def test_list_medicine_api(self,make_obj,pagesize,pagenum):
        rs = make_obj.list_medicine_api(pagesize,pagenum)
        print(rs.text)
        logger.info(rs.text)
        assert rs.status_code == 200
        

class Test_add_medicine:
    data = Utils.yaml_load('case/login_test.yaml')

    
    @allure.feature('添加产品')   
    @pytest.mark.parametrize('name,desc,sn',Utils.yaml_data(data,'test_add_medicine'))
    def test_add_medicine(self,delete_medicine_in_teardown,name,desc,sn):
        rs = delete_medicine_in_teardown.add_medicine(name,desc,sn)
        res = rs.json()
        print(res)
        logger.error(res)
        assert rs.status_code == 200
        delete_medicine_in_teardown.id = res['id']# 这一步很巧妙！直接把动态产生的数据绑定到了medicine的属性中，这样
        # time.sleep(5)

# class Test_delete_medicine:

#     def setup_method(self):
#         # 创建medicine对象
#         self.medicine_obj = Medicine()
#         # 创建药品，这里的思路是要随机产生药品，工厂类